﻿using Sinacor.OMM.Util;

namespace Sinacor.OMM.Deployment.Setup.Steps
{
    public class InstallWindowsRegistry
    {
        #region [ Constants ]

        const string localRoot = "HKEY_LOCAL_MACHINE";
        const string subkey = "SOFTWARE";
        const string sinacorKey = "SINACOR";
        const string dataKey = "Infra.Data";
        const string keyName = localRoot + "\\" + subkey + "\\" + sinacorKey + "\\" + dataKey + "\\";
        const string instance = keyName + "Instances";
        const string config = keyName + "Config";

        #endregion

        #region [ Public Methods ]

        public string GetRegistryValue(string valueName, bool isInstance)
        {
            if(isInstance)
                return WindowsRegistry.GetRegistryValue(instance, valueName);
            else
                return WindowsRegistry.GetRegistryValue(keyName, valueName);
        }

        public string DecryptPasswordValue(string password)
        {
            return EncryptDecrypt.Decrypt(password);
        }

        public string EncryptPasswordValue(string password)
        {
            return EncryptDecrypt.Encrypt(password);
        }

        public void CreateSinacorRegistry(string user, string password, string tns)
        {
            WindowsRegistry.CreateRegistry(keyName, "ConnectionLifetime", "120");
            WindowsRegistry.CreateRegistry(keyName, "ConnectionTimeout", "60");
            WindowsRegistry.CreateRegistry(keyName, "DecrPoolSize", "1");
            WindowsRegistry.CreateRegistry(keyName, "IncrPoolSize", "5");
            WindowsRegistry.CreateRegistry(keyName, "MaxPoolSize", "15");
            WindowsRegistry.CreateRegistry(keyName, "MinPoolSize", "1");
            WindowsRegistry.CreateRegistry(keyName, "Password", password);
            WindowsRegistry.CreateRegistry(keyName, "Pooling", "true");
            WindowsRegistry.CreateRegistry(keyName, "TimeOut", "3600");
            WindowsRegistry.CreateRegistry(keyName, "User", user);

            WindowsRegistry.CreateRegistry(config, "IsEncrypted", "0");

            WindowsRegistry.CreateRegistry(instance, "Default", tns);
        }

        #endregion
    }
}